package org.ubc.cs410.disposal2020;

import javax.jdo.PersistenceManager;
import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.IdentityType;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

import com.google.appengine.api.datastore.Key;

/*
 * Robot Class
 * In the game, the players control five robots to collect garbages
 */

@PersistenceCapable(identityType = IdentityType.APPLICATION)
public class Robot implements HasLocation {

	@PrimaryKey
	@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
	private Key key;
	@Persistent(defaultFetchGroup="true")
	private Player player;
	@Persistent
	private Float latitude;
	@Persistent
	private Float longitude;
	@Persistent
	private boolean movedThisTurn;

	private static final double MOVEMENT_RANGE = 200;
	public static final double GARBAGE_COLLECTION_RADIUS = 50;

	public Robot(Player player) {
		this.player = player;
		this.latitude = player.getFactoryLat();
		this.longitude = player.getFactoryLng();
		this.movedThisTurn = false;
	}

	public void makeMovable() {
		this.movedThisTurn = false;
	}

	public boolean isMovable() {
		return !this.movedThisTurn;
	}

	public Key getKey() {
		return key;
	}

	public Player getPlayer(){
		return player;
	}

	public double getLatitude() {
		return latitude;
	}

	public double getLongitude() {
		return longitude;
	}

	public void move(double newLat, double newLng) {
		Location loc = new Location(newLat,newLng);
		if (isMovable()) {
			if (Distance.calculate(loc, this)<MOVEMENT_RANGE) {
				this.latitude = new Float(newLat);
				this.longitude = new Float(newLng);
				this.movedThisTurn = true;
			}
		}
	}
}
